//
//  KAUser.swift
//  kano
//
//  Created by Sophia on 2024/8/20.
//

import KakaJSON

struct KAUser: Convertible, Codable {
    var userGid: String = "" //业务端使用
    var userStatus: Int = 0
    var t: String = ""
    var u: Int = 0
    var userId: Int = 0   //utc验证使用
    var userid: Int = 0
    var uid: Int = 0
    var isLogin: Bool = false
    var freezeStatus: Int = 0
    var name: String = ""
    var sessionId: String = ""
    var installTime: String = ""
    var mobile: String = ""
    var loginFromType: String = ""
    var userToken: String = ""
    var verifyToken: String = ""
    var codeToken: String = ""
    var currentBizArg: String = ""
    var havapassword: Bool = false
    var haveregistered: Bool = false
    var litePackageName: String = ""
    var registerC: Int = 0
    var logoutStatus: Int = 0
    var channelUserGid: String = ""
    var invitePrizeResp: String = ""
    var isHuidus: String = ""
    var setPwdToken: String = ""
    var userType: String = ""
    
    mutating func kj_didConvertToModel(from json: [String : Any]) {
        if !userToken.isEmpty {
            t = userToken
        }
        
        if userid != 0 {
            userId = userid
            u = userid
        } else if userId != 0 {
            userid = userId
            u = userId
        }
    }
    
    func merge(with other: KAUser) -> KAUser {
        var user = KAUser(
            userGid: !other.userGid.isEmpty ? other.userGid : self.userGid,
            userStatus: other.userStatus != 0 ? other.userStatus : self.userStatus,
            t: !other.t.isEmpty ? other.t : self.t,
            u: other.u != 0 ? other.u : self.u,
            userId: other.userId != 0 ? other.userId : self.userId,
            userid: other.userid != 0 ? other.userid : self.userid,
            uid: other.uid != 0 ? other.uid : self.uid,
            isLogin: other.isLogin,
            freezeStatus: other.freezeStatus != 0 ? other.freezeStatus : self.freezeStatus,
            name: !other.name.isEmpty ? other.name : self.name,
            sessionId: !other.sessionId.isEmpty ? other.sessionId : self.sessionId,
            installTime: !other.installTime.isEmpty ? other.installTime : self.installTime,
            mobile: !other.mobile.isEmpty ? other.mobile : self.mobile,
            loginFromType: !other.loginFromType.isEmpty ? other.loginFromType : self.loginFromType,
            userToken: !other.userToken.isEmpty ? other.userToken : self.userToken,
            verifyToken: !other.verifyToken.isEmpty ? other.verifyToken : self.verifyToken,
            codeToken: !other.codeToken.isEmpty ? other.codeToken : self.codeToken,
            currentBizArg: !other.currentBizArg.isEmpty ? other.currentBizArg : self.currentBizArg,
            havapassword: other.havapassword,
            haveregistered: other.haveregistered,
            litePackageName: !other.litePackageName.isEmpty ? other.litePackageName : self.litePackageName,
            registerC: other.registerC != 0 ? other.registerC : self.registerC,
            logoutStatus: other.logoutStatus != 0 ? other.logoutStatus : self.logoutStatus,
            channelUserGid: !other.channelUserGid.isEmpty ? other.channelUserGid : self.channelUserGid,
            invitePrizeResp: !other.invitePrizeResp.isEmpty ? other.invitePrizeResp : self.invitePrizeResp,
            isHuidus: !other.isHuidus.isEmpty ? other.isHuidus : self.isHuidus,
            setPwdToken: !other.setPwdToken.isEmpty ? other.setPwdToken : self.setPwdToken,
            userType: !other.userType.isEmpty ? other.userType : self.userType
        )
        
        //login -> uid
        if user.userid != 0 {
            user.userId = user.userid
            user.u = user.userid
        }
        //register -> userId
        else if user.userId != 0 {
            user.userid = user.userId
            user.u = user.userId
        }
        
        return user
    }
}

